﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using Entity;

namespace SalesVMT
{
    public partial class frmOrigin : Form
    {
        DialogResult dlgResult;
        OriginInfo origininf;
        public frmOrigin(int _Origin_ID)
        {
            InitializeComponent();
            origininf = new OriginInfo();
            
            if (_Origin_ID > 0)
            {
                origininf = OriginDB.GetInfo(_Origin_ID);
                
                InfoToForm();
            }
            origininf.Origin_ID = _Origin_ID;
            this.KeyPreview = true;
            dlgResult = DialogResult.Cancel;
        }
        private void FormToInfo()
        {
            origininf.Origin_Name = txtName.Text;
        }
        private void InfoToForm()
        {

            txtName.Text = origininf.Origin_Name;

        }
        private bool CheckValid()
        {
            if (txtName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Nơi xuất xứ trống! Hãy nhập lại.", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtName.Focus();
                return false;
            }
            return true;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if(!CheckValid()) return;
            FormToInfo();
            if (origininf.Origin_ID > 0)//Update
            {
                if (OriginDB.Exist(origininf.Origin_ID, txtName.Text))
                {
                    MessageBox.Show(Message.Exist, Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (OriginDB.Update(origininf))
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Update_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(Message.Update_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else //Create
            {
                if (OriginDB.Exist(origininf.Origin_ID, txtName.Text))
                {
                    MessageBox.Show(Message.Exist, Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                origininf.Origin_ID = OriginDB.Insert(origininf);
                if (origininf.Origin_ID > 0)
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Create_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(Message.Create_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmOrigin_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = dlgResult;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            
            txtName.Text = "";

            origininf.Origin_ID = 0;
        }

        private void frmOrigin_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Escape:
                    this.Dispose();
                    this.DialogResult = dlgResult;
                    this.Close();
                    break;
            }
        }

    }
}